package com.mljqqh.controller;

import com.leyou.item.pojo.Category;
import com.mljqqh.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/*

 */
@Controller
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoryById(@RequestParam( value = "pid" ,defaultValue = "0") Long pid){

        //参数列表错误：400
        if (null == pid || pid<0){
            return ResponseEntity.badRequest().build();
        }

        //查询返回数据
        List<Category> categories = this.categoryService.queryCategoryById(pid);
        if (CollectionUtils.isEmpty(categories)){
            //返回404 没有找到
            return ResponseEntity.notFound().build();
        }
        //查询成功 返回 200

        return ResponseEntity.ok(categories);





    }

    @GetMapping("hi")
    @ResponseBody
    public String fun(){

        return "hello";
    }


}

